package fr.mk25.audio{
    import flash.media.Sound;
    import flash.media.SoundChannel;
    import flash.media.SoundTransform;
    import flash.media.SoundLoaderContext;
    import flash.media.ID3Info;
    import flash.net.URLRequest;
    import flash.events.EventDispatcher;

    public class Mp3FileHandler extends EventDispatcher {
        private var mp3File:Sound;
        private var soundChannel: SoundChannel;
        private var soundTransform: SoundTransform;
        private var context : SoundLoaderContext;
        private var urlRequest:URLRequest;
        private var elapsedTime:int;
        private var status:int;

        public static var PLAYING:int       = 0;
        public static var PAUSED:int        = 1;
        public static var STOPPED:int       = 2;
        public static var INSTANCIATED:int  = 3;

        public function Mp3FileHandler(url:URLRequest=null, st:SoundTransform=null, context:SoundLoaderContext=null){
            mp3File = new Sound(url, context);
            elapsedTime=0;
            status = Mp3FileHandler.INSTANCIATED;
            this.context = context;
            soundTransform = st;
    
        }

        public function play():SoundChannel{
            soundChannel = mp3File.play(elapsedTime, 0, soundTransform);
            status = Mp3FileHandler.PLAYING;
            return soundChannel;
        }

        public function playAt(startTime:int):SoundChannel{
            elapsedTime = startTime;
            return this.play();
        }

        public function stop():void{
            soundChannel.stop();
            status = Mp3FileHandler.STOPPED;
        }

        public function replay():SoundChannel{
            this.stop();
            mp3File = new Sound();
            elapsedTime=0;
            return this.play();
        }

        public function pause():void{
            elapsedTime = soundChannel.position;
            this.stop();
            status = Mp3FileHandler.PAUSED;
        }

        public function getStatus():int{
            return status;
        }

        public function getMedia():Sound{
            return mp3File;
        }
   
        public function getSoundTransform():SoundTransform{
            return soundTransform;
        }

        public function changeSoundTransform(st:SoundTransform):void{
            elapsedTime = soundChannel.position;
            soundTransform = st;
            soundChannel.stop();
            this.play(); 
        }

        public function getID3():ID3Info{
            return mp3File.id3;
        }
    

        public function getProgress():Number{
            try{
                if(status == Mp3FileHandler.PLAYING ) return soundChannel.position;
            }catch(error:TypeError){}
            return 0;
        }   

        public function getProgressAsPercentage():Number{
            try{
                return this.getProgress()/mp3File.length;
            }catch(error:TypeError){}
            return 0;
        }

 
        public function load(url:URLRequest):void{
            mp3File.load(url, context);
            urlRequest = url;
            this.dispatchEvent( new MusicFileEvent(MusicFileEvent.LOAD_COMPLETE) )
        }
    }
}
